//
// Copyright 2001-2004 Stottler Henke Associates, Inc.
// All Rights Reserved.
////////////////////////////////////////////////////////////////////////////////

package com.stottlerhenke.simbionic.engine.parser;
import com.stottlerhenke.simbionic.common.*;

import com.stottlerhenke.simbionic.engine.SB_SingletonBook;
import com.stottlerhenke.simbionic.engine.file.SB_StreamReader;
import com.stottlerhenke.simbionic.api.*;
import com.stottlerhenke.simbionic.api.SB_Param;

/**
 * Represents a single variable of type data.
 */
public class SB_VarData extends SB_Variable 
{

  public SB_VarData() {
    _parameterValue = new SB_Param( (Object) null );
  }
  public SB_VarData(SB_VarData var){
    Copy(var);
  }
  public SB_VarData(Object var){
    _parameterValue = new SB_Param( var );
  }
  public SB_Variable opAsign( SB_VarData var) {
    if (this != var) {
      Copy(var);
    }
    return this;
  }

  public SB_Variable Clone() throws SB_Exception {
    return new SB_VarData(this);
  }

  public void load(SB_StreamReader reader,SB_SingletonBook book) 
		throws SB_FileException
	{
    // junk value, can't load pointers from file
    reader.readInt();
  }

  private void Copy( SB_VarData var) {
    // don't know how to deep copy raw data pointers,
    // so just share them
    _parameterValue = new SB_Param( var.GetValue() );
  }


  public Object GetValue() {
          return _parameterValue.getData();
  }
  
  public void setSB_Param(SB_Param newParam) throws SB_Exception
  { 
    _parameterValue = newParam;
  }
  
  public SB_VarType GetType() { return SB_VarType.kData; }
  
  public void SetValue(Object value){
    // not responsible for deallocated raw data pointers
     _parameterValue.setData(value);
  }

  public static Object GET_DATA(SB_Variable var){
    return var.getSB_Param().getData();
  }
  
  public SB_Variable opOR( SB_Variable arg2)  {
    //FATAL( false, "Invalid operation for data variable");
    return null;
  }

  public SB_Variable opAND( SB_Variable arg2) throws SB_Exception  {
    //FATAL( false, "Invalid operation for data variable");
    return null;
  }

  public SB_Variable opGRTEq( SB_Variable arg2) throws SB_Exception  {
    //FATAL( false, "Invalid operation for data variable");
    return null;
  }

  public SB_Variable opLSSEq( SB_Variable arg2) throws SB_Exception  {
    //FATAL( false, "Invalid operation for data variable");
    return null;
  }

  public SB_Variable opNOTEq( SB_Variable arg2) throws SB_Exception  {
    SB_Variable result = null;
    if(arg2.GetType() == SB_VarType.kData)
      result = new SB_VarBoolean( GetValue() != GET_DATA(arg2) );
    else if(arg2.GetType() == SB_VarType.kInvalid)
      result = new SB_VarBoolean( true );
    return result;
  }

  public SB_Variable  equals( SB_Variable arg2) throws SB_Exception  {
    SB_Variable result = null;

    if(arg2.GetType() == SB_VarType.kData)
      result = new SB_VarBoolean( GetValue() == GET_DATA(arg2) );
    else if(arg2.GetType() == SB_VarType.kInvalid)
      result = new SB_VarBoolean( false );
    return result;
  }

  public SB_Variable opGRT( SB_Variable arg2) throws SB_Exception  {
    //FATAL( false, "Invalid operation for data variable");
    return null;
  }

  public SB_Variable opLSS( SB_Variable arg2) throws SB_Exception  {
    //FATAL( false, "Invalid operation for data variable");
    return null;
  }

  public SB_Variable opMOD( SB_Variable arg2) throws SB_Exception  {
    //FATAL( false, "Invalid operation for data variable");
    return null;
  }

  public SB_Variable opEXP( SB_Variable arg2) throws SB_Exception  {
    //FATAL( false, "Invalid operation for data variable");
    return null;
  }

  public SB_Variable opDVI( SB_Variable arg2)  {
    //FATAL( false, "Invalid operation for data variable");
    return null;
  }

  public SB_Variable opMUL( SB_Variable arg2) throws SB_Exception  {
    //FATAL( false, "Invalid operation for data variable");
    return null;
  }

  public SB_Variable opSUB( SB_Variable arg2) throws SB_Exception  {
    //FATAL( false, "Invalid operation for data variable");
     return null;
  }

  public SB_Variable opADD( SB_Variable arg2) throws SB_Exception  {
    SB_Variable result = null;
    if(arg2.GetType() == SB_VarType.kString)	// returns a concatenated string
    {
      String workStr;
      workStr = GetValue() +  SB_VarString.GET_STRING(arg2);
      result = new SB_VarString( workStr );
    }
    return result;
  }

  public SB_Variable opNOT () throws SB_Exception {
    return new SB_VarBoolean( GetValue() == null );
  }

  public SB_Variable opNEG () throws SB_Exception {
    //FATAL( false, "Invalid operation for data variable");
    return null;
  }

  public boolean bool() throws SB_Exception {
    return GetValue() != null;
  }


  public String toString(){
   return new String("kData: " + GetValue());
  }

}